import * as zrender from 'zrender';
import { unitWidth, halfUnitWidth } from '../config';
import { useCalendarStore } from '../../stores/calendar';

/**
 * 绘制今天线
 * @param {object} zr ZRender实例
 * @param {number} chartStartX 图表起始X
 * @param {number} chartStartY 图表起始Y
 * @param {number} timeScaleHeight 时间轴高度
 * @param {number} todayOffset 今天偏移量
 * @param {object} chartState 图表状态对象
 */
export function drawTodayLine(
  zr: any,
  chartStartX: number,
  chartStartY: number,
  timeScaleHeight: number,
  todayOffset: number,
  chartState: ChartState,
) {
  // 检查今天是否在时间范围内
  const calendarStore = useCalendarStore();
  const startDate = new Date(calendarStore.dateRange.start);
  const endDate = new Date(calendarStore.dateRange.end);
  const today = new Date();

  if (today < startDate || today > endDate) {
    return;
  }

  const todayLine = new zrender.Line({
    shape: {
      x1: chartStartX + todayOffset * unitWidth + halfUnitWidth,
      y1: chartStartY,
      x2: chartStartX + todayOffset * unitWidth + halfUnitWidth,
      y2: chartState.canvasHeight,
    },
    style: {
      stroke: '#2955c9',
      lineDash: [5, 5],
      lineWidth: 2,
    },
    z: -1,
  });
  const circle = new zrender.Circle({
    shape: {
      cx: chartStartX + todayOffset * unitWidth + halfUnitWidth,
      cy: chartStartY - timeScaleHeight + timeScaleHeight + 2,
      r: 2,
    },
    style: {
      fill: '#2955c9',
    },
  });
  zr.add(todayLine);
  zr.add(circle);
}
